<?php
/**
 * Alist网盘直链
 * @Copyright (C) 2024 比特福 All rights reserved.
 * @License http://tool.bitefu.net
 * @Author xiaogg <xiaogg@sina.cn>
 */
Class Alist
{
    public $Api = 'http://alist.domain.com'; // 你的 alist 地址
    private $UserName = '账号'; // 账号
    private $PassWord = '密码'; // 密码
    private $UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36';
 
    // token获取
    public function getToken(){
        $cacheName = 'alist_'.$this->UserName.'_token';
        if(is_file($cacheName)){
            $cachedata = json_decode(file_get_contents($cacheName),true);
            if(!empty($cachedata['token']) && $cachedata['expire_time']>time())return $cachedata['token'];
        }
        $Url = $this->Api.'/api/auth/login';
        $Body = '{"username": "'.$this->UserName.'","password": "'.$this->PassWord.'"}';
        $Data = $this->request($Url,'POST','',$Body);
        if($Data['code'] == 200){
            $Token = $Data['data']['token'];
            $Data=[];
            $Data['token']=$Token;
            $Data['expire_time']=time()+60*60*48;
            file_put_contents($cacheName,json_encode($Data));
            return $Token;
        }else{
            exit('ERROR:'.$Data['message']);
        }
    }
 
    // 获取当前用户信息
    public function getUserInfo(){
        $Url = $this->Api.'/api/me';
        $Header = array(
            'Authorization:'.$this->getToken(),
        );
        $Data = $this->request($Url,'GET',$Header);
        return $Data;
    }
    // POST 列出文件目录
    public function lists($path,$password='',$page='',$per_page='',$refresh='false'){
        $Url = $this->Api.'/api/fs/list';
        $Header = array(
            'Authorization:'.$this->getToken(),
        );
        $param = array(
            'path'=>$path,
            'password'=>$password,
            'page'=>$page,
            'per_page'=>$per_page,
            'refresh'=>$refresh,
        );
        $Body = http_build_query($param);
        $Data = $this->request($Url,'POST',$Header,$Body);
        return $Data;
    }
 
    // POST 获取某个文件/目录信息
    public function get( $path, $password='', $page='', $per_page='', $refresh='false'){
        $Url = $this->Api.'/api/fs/get';
        $Header = array(
            'Authorization:'.$this->getToken(),
        );
        $param = array(
            'path'=>$path,
            'password'=>$password,
            'page'=>$page,
            'per_page'=>$per_page,
            'refresh'=>$refresh,
        );
        $Body = http_build_query($param);
        $Data = $this->request($Url,'POST',$Header,$Body);
        return $Data;
    }
 
    private function request($Url, $Method="GET", $Header='', $Data='')
    {
        $Ch = curl_init();
        $Header = $Header ? $Header : array("Content-Type:application/json;charset=utf-8;","Accept:application/json","charset=utf-8");
        $Data = is_array($Data) ? json_encode($Data) : $Data;
        curl_setopt($Ch, CURLOPT_URL, $Url);
        curl_setopt($Ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($Ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if($Method == "GET"){
            curl_setopt($Ch, CURLOPT_CUSTOMREQUEST,"GET");
            curl_setopt($Ch, CURLOPT_POST, false);
            curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
        }
        if($Method == "POST"){
            curl_setopt($Ch, CURLOPT_CUSTOMREQUEST,"POST");
            curl_setopt($Ch, CURLOPT_POST, true);
            curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
        }
        if($Method == "PUT"){
            curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
        }
        if($Method == "DELETE"){
            curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
        }
        if($Method == "PATCH"){
            curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "PATCH");
            curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data);
        }
        curl_setopt($Ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($Ch, CURLOPT_HTTPHEADER, $Header);
        curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($Ch, CURLOPT_ENCODING, 'gzip');
        $Output = curl_exec($Ch);
        curl_close($Ch);
        $result= json_decode($Output,1);
        if(!empty($result['code']) && $result['code']==401){
            $cacheName = 'alist_'.$this->UserName.'_token';
            file_put_contents($cacheName,json_encode(array()));
        }
        return $result;
    }
}
 
?>